package com.beijing.demo.find;

/**
 * 曹旭
 * 2025/4/24
 * 两两元素不相等的数组,找出数组中的一个峰值peak
 */
public class FindPeakElement {

    public static void main(String[] args) {
        int arr[] = {1,2,3,1};
        getPeakElement(arr);
    }

    /**
     * 找出峰值
     * @param arrs
     * @return
     */
    public static int getPeakElement(int[] arrs){
        if(arrs.length == 1){
            return 0;
        }
        if(arrs[0] > arrs[1]){
            return 0;
        }
        if(arrs[arrs.length - 1] > arrs[arrs.length - 2]){
            return arrs.length - 1;
        }
        int l = 1,r = arrs.length - 2,m = 0,result = -1;
        while(l <= r){
            m = l + ((r - l) >> 1);
            if(arrs[m - 1] > arrs[m]){
                r = m - 1;
            }else if(arrs[m + 1] > arrs[m]){
                l = m + 1;
            }else {
                result = m;
                break;
            }

        }
        return result;
    }
}
